package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.MesBom;
import com.ruoyi.system.service.IMesBomService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * BOMController
 * 
 * @author ruoyi
 * @date 2024-06-01
 */
@RestController
@RequestMapping("/system/bom")
public class MesBomController extends BaseController
{
    @Autowired
    private IMesBomService mesBomService;

    /**
     * 查询BOM列表
     */
    @PreAuthorize("@ss.hasPermi('system:bom:list')")
    @GetMapping("/list")
    public TableDataInfo list(MesBom mesBom)
    {
        startPage();
        List<MesBom> list = mesBomService.selectMesBomList(mesBom);
        return getDataTable(list);
    }

    /**
     * 导出BOM列表
     */
    @PreAuthorize("@ss.hasPermi('system:bom:export')")
    @Log(title = "BOM", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MesBom mesBom)
    {
        List<MesBom> list = mesBomService.selectMesBomList(mesBom);
        ExcelUtil<MesBom> util = new ExcelUtil<MesBom>(MesBom.class);
        util.exportExcel(response, list, "BOM数据");
    }

    /**
     * 获取BOM详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:bom:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {

        return AjaxResult.success(mesBomService.selectMesBomById(id));
    }

    /**
     * 新增BOM
     */
    @PreAuthorize("@ss.hasPermi('system:bom:add')")
    @Log(title = "BOM", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MesBom mesBom)
    {
        if (!mesBomService.isBomIdUnique(mesBom,true))
        {
            return AjaxResult.error("bom编码重复");
        }
        return toAjax(mesBomService.insertMesBom(mesBom));
    }

    /**
     * 修改BOM
     */
    @PreAuthorize("@ss.hasPermi('system:bom:edit')")
    @Log(title = "BOM", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MesBom mesBom)
    {
        //处理逻辑
        //编码不能重复
        if (!mesBomService.isBomIdUnique(mesBom,false))
        {
            return AjaxResult.error("bom编码重复");
        }

        return toAjax(mesBomService.updateMesBom(mesBom));
    }

    /**
     * 删除BOM
     */
    @PreAuthorize("@ss.hasPermi('system:bom:remove')")
    @Log(title = "BOM", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(mesBomService.deleteMesBomByIds(ids));
    }
}
